﻿using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Project_Flight;
using Sybase.Mobile;
using Sybase.Persistence;
using Sybase.Collections;

namespace Project_Flight
{
    public partial class FormCustomer : Form
    {
        private const String USERNAME = "supAdmin";
        private const String PASSWORD = "nofear89";
        private const String HOST = "abdelrahman-pc";
        private const int PORT = 5001;
        private const int TIMEOUT = 600;
        public FormCustomer()
        {
            InitializeComponent();
        }
        private void AddString(String s)
        {
            textBox1.Text += s + "\r\n";
            textBox1.SelectionStart = textBox1.Text.Length;
            textBox1.ScrollToCaret();
            textBox1.Refresh();
        }
        private void AddListView()
        {
            this.listView1.Clear();
            listView1.Columns.Add("ID", listView1.Width / 4, HorizontalAlignment.Left);
            listView1.Columns.Add("Customer name", listView1.Width / 3, HorizontalAlignment.Center);
            listView1.Columns.Add("City", listView1.Width / 3, HorizontalAlignment.Right);
            listView1.View = View.Details;
            listView1.FullRowSelect = true;
        }


        private void button1_Click(object sender, EventArgs e)
        {
            Sybase.Mobile.Application app = Sybase.Mobile.Application.GetInstance();
            app.ApplicationIdentifier = "flight";
            // MyCallbackHandler.textBox1 = textBox1;
            //FlightDB.RegisterCallbackHandler(new MyCallbackHandler());
            FlightDB.SetApplication(app);
            FlightDB.GetSynchronizationProfile().ServerName = HOST;

            ConnectionProperties connProps = app.ConnectionProperties;
            LoginCredentials loginCredentials = new LoginCredentials(USERNAME, PASSWORD);

            connProps.LoginCredentials = loginCredentials;
            connProps.ServerName = HOST;
            connProps.PortNumber = PORT;

            if (app.RegistrationStatus != RegistrationStatus.REGISTERED)
            {
                AddString("Application registering ... ");
                app.RegisterApplication(TIMEOUT);
                AddString("Application registered");
            }
            else
            {
                AddString("Connecting to server ...");
                app.StartConnection(TIMEOUT);
                AddString("Connected to server");
            }

            if (!FlightDB.IsSynchronized("default"))
            {
                FlightDB.DisableChangeLog();
                AddString("Package synchronizing ...");
                FlightDB.Synchronize(); // Initial Synchronize

                ISynchronizationGroup sg = FlightDB.GetSynchronizationGroup("default");
                sg.EnableSIS = true;
                sg.Save();
                FlightDB.Synchronize();
                AddString("Package synchronized");
            }
            FlightDB.EnableChangeLog();
            AddListView();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (Sybase.Mobile.Application.GetInstance().RegistrationStatus == RegistrationStatus.REGISTERED)
            {
                Cursor.Current = Cursors.WaitCursor;
                AddString("Loading data from database ...");
                AddDataToListView();
                Cursor.Current = Cursors.Default;
            }
            else
            {
                AddString("Application is not initialized!");
            }


        }

        private void AddDataToListView()
        {
            this.listView1.Clear();
            listView1.Columns.Add("ID", listView1.Width / 4,
            HorizontalAlignment.Left);
            listView1.Columns.Add("Customer name", listView1.Width /
            3, HorizontalAlignment.Center);
            listView1.Columns.Add("City", listView1.Width / 3,
            HorizontalAlignment.Right);
            listView1.View = View.Details;
            listView1.FullRowSelect = true;

            GenericList<MBO_Customer> customers = MBO_Customer.FindAll();

            foreach (MBO_Customer customer in customers)
            {
                String id = customer.CUSTOMERID;
                String name = customer.CUSTNAME;
                String city = customer.CITY;
                ListViewItem item = new ListViewItem(id);
                item.SubItems.Add(name);
                item.SubItems.Add(city);
                listView1.Items.Add(item);

            }


        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (listView1.FocusedItem != null)
            {
                Program.setCustomer(listView1.FocusedItem.Text);
                MessageBox.Show(Program.getCustomer());
                Program.getForm2().Visible = true;
                Program.getForm1().Visible = false;
            }
            else
                MessageBox.Show("Please select a row");
        }
    }
}